package work.hashmap;

import java.util.*;

/**
 * @author WDH
 * @description
 * @date 2025年01月12日 16:13
 */

public class Problem202 {

    public boolean isHappy(int n) {

        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = createMap(n);
        }
        if (n == 1) return true;
        else return false;

    }


    public int createMap(int n) {

        List<Integer> list = new ArrayList<>();
        while (n >= 1) {
            int a = n % 10;
            n = n / 10;
            list.add(a);
        }
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum = sum + list.get(i) * list.get(i);
        }
        return sum;
    }


    public static void main(String[] args) {
        Problem202 problem202 = new Problem202();
        problem202.isHappy(19);
    }
}
